﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Web.UI.HtmlControls;

namespace GMS
{
    public partial class Pages_SelectStoneCategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (SessionVariable.Instance.UserDetails.SelectedStoneTypeId > 0 &&
                    SessionVariable.Instance.UserDetails.ShapeId > 0)
                {
                    dvMain.Visible = true;
                    rptrSelectStoneCategory.DataSource = Common.GetStoneTypeCategoryWithPrice(SessionVariable.Instance.UserDetails.SelectedStoneTypeId);
                    rptrSelectStoneCategory.DataBind();
                }
                else
                {
                    dvMain.Visible = false;

                    if (SessionVariable.Instance.UserDetails.SelectedStoneTypeId <= 0)
                    {
                        ShowMessage("Please select a surface first.");
                    }
                    else if (SessionVariable.Instance.UserDetails.ShapeId <= 0)
                    {
                        ShowMessage("Please select a shape first.");
                    }
                }
            }
        }

        protected void rptrSelectStoneCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ImageButton imgStoneType = (ImageButton)e.Item.FindControl("imgStoneType");
                
                using (HtmlGenericControl dvImg = (HtmlGenericControl)e.Item.FindControl("dvImg"))
                {
                    if (dvImg != null)
                    {
                        dvImg.Attributes["onmouseover"] = "this.style.borderColor='Red'; this.style.borderWidth='2px'";
                        dvImg.Attributes["onmouseout"] = "this.style.borderColor='transparent'";
                    }
                }
                if (!string.IsNullOrEmpty(imgStoneType.AlternateText))
                {
                    if (Convert.ToInt32(imgStoneType.AlternateText) == SessionVariable.Instance.UserDetails.SelectedStoneCategoryId)
                    {
                        using (HtmlGenericControl dvImg = (HtmlGenericControl)e.Item.FindControl("dvImg"))
                        {
                            if (dvImg != null)
                            {
                                dvImg.Style.Add(HtmlTextWriterStyle.BorderColor, "Green");
                                dvImg.Style.Add(HtmlTextWriterStyle.BorderWidth, "4px");
                            }
                        }
                    }
                }
            }
        }

        protected void rptrSelectStoneCategory_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) &&
                string.Compare(e.CommandName.ToLower(), "click") == 0)
            {
                SessionVariable.Instance.UserDetails.SelectedStoneCategoryId = Convert.ToInt32(e.CommandArgument);

                using (Repeater rptr = (Repeater)e.Item.Parent)
                {
                    if (rptr != null)
                    {
                        foreach (RepeaterItem item in rptr.Items)
                        {
                            using (HtmlGenericControl dvImg = (HtmlGenericControl)item.FindControl("dvImg"))
                            {
                                if (dvImg != null)
                                {
                                    dvImg.Style.Add(HtmlTextWriterStyle.BorderColor, "transparent");
                                }
                            }
                        }
                    }
                }

                using (HtmlGenericControl dvImg = (HtmlGenericControl)e.Item.FindControl("dvImg"))
                {
                    if (dvImg != null)
                    {
                        dvImg.Style.Add(HtmlTextWriterStyle.BorderColor, "Green");
                        dvImg.Style.Add(HtmlTextWriterStyle.BorderWidth, "4px");
                        nextPage();
                    }
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("EnterShapeInputs.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            nextPage();
        }

        private void nextPage()
        {
            if (SessionVariable.Instance.UserDetails.SelectedStoneCategoryId > 0)
            {
                Response.Redirect("SelectColor.aspx");
            }
            else
            {
                ShowMessage("Please select a stone category.");
            }
        }

        private void ShowMessage(string msgText)
        {
            HtmlGenericControl divMsg = (HtmlGenericControl)Page.Master.FindControl("divMsg");

            if (divMsg != null)
            {
                Control msg = this.Page.LoadControl("Controls/Message.ascx");

                if (msg != null)
                {
                    Label lblMessage = (Label)msg.FindControl("lblMessage");

                    if (lblMessage != null)
                    {
                        lblMessage.Text = msgText;
                    }

                    divMsg.Controls.Add(msg);
                }
            }
        }
    }
}